﻿/*********************************************************************************************************************
VanguardDocumentation, Procedure SetCaption

© 2014 Erik Ackerholm

This file is part of SqlVanguard.

SqlVanguard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

SqlVanguard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with SqlVanguard. If not, see 
<http://www.gnu.org/licenses/>.
*********************************************************************************************************************/

if
	not exists (
		select *
		from sys.schemas as s
			inner join sys.objects as o on (s.schema_id = o.schema_id)
		where s.name = 'VanguardDocumentation' and o.[type] = 'P'
			and o.name = 'SetCaption'
	)
begin
	declare @SqlCommand as nvarchar(max);
	set @SqlCommand = '
		create procedure VanguardDocumentation.SetCaption
		as raiserror (''Procedure SetCaption is not implemented yet!'', 11, 1);
	';
	execute (@SqlCommand);
end;
GO

alter procedure VanguardDocumentation.SetCaption
	/*****************************************************************************************************************
	Stores a caption for an entity.
	*****************************************************************************************************************/
	
	@EntityName                 nvarchar(512),
		/* Dot-separated name of entity which the caption is for. */
	@Caption                    nvarchar(3750),
		/* Caption to store. The maximum allowed length is further constrained within the procedure. Also, line breaks 
		are not permitted. Use null to remove a previously set caption. */
	@EntityType                 varchar(128)                = null
		/* Optionally give the type of entity, if there are many entities with the same name. */
	
	with execute as owner
as
begin
	
	set nocount on;
	set xact_abort on;
	set lock_timeout 3000;
	set transaction isolation level read committed;
	
	declare @ErrorNo as integer;
	declare @ErrorMsg as nvarchar(1536);
	declare @ErrorLine as integer;
	declare @ErrorState as integer;
	
	declare @MaximumLength as integer;
	declare @ActualLength as integer;
	
	begin try;
		
		-- Planning to put @MaximumLength in a table so that it may be changed to fit the need of the user
		-- but for now it is hardcoded at 120 characters
		set @MaximumLength = 120;
		set @ActualLength = len(@Caption);
		
		-- Check the format of @Caption
		if
			charindex(char(10), @Caption) > 0 or charindex(char(13), @Caption) > 0
		begin
			raiserror (740909, @EntityName, @EntityType);
			return;
		end
		else if
			@ActualLength > @MaximumLength
		begin
			raiserror (740910, @EntityName, @EntityType, @MaximumLength, @ActualLength);
			return;
		end;
		
		-- Store caption with SetTextProperty
		execute VanguardDocumentation.SetTextProperty @EntityName, 'Caption', @Caption, @EntityType;
		
	end try
	begin catch;
		
		set @ErrorNo = error_number();
		set @ErrorMsg = left(error_message(), 1536);
		set @ErrorLine = error_line();
		set @ErrorState = error_state();
		
		if @@trancount > 0
			rollback transaction;
		
		raiserror (740900, 16, @ErrorState, 'SetCaption', @ErrorNo, @ErrorLine, @ErrorMsg);
		
	end catch;
	
end;
GO




